/**
 * 用户相关服务
 */
import wx from 'wx'
import api from '@/utils/api'
import util from '@/utils/util'
/**
 * 调用微信登录
 */
function loginBypassword (e) {
  // let code = null
  return new Promise(function (resolve, reject) {
    // return util.login().then((res) => {
    //   code = res.code
    // }).then((userInfo) => {
    // 把用户信息传给后台，存入数据库，并计算一个token给前台存起来
    util.request(api.AuthLoginByWeixin, {
      // code: code,
      orcode: e.orcode,
      username: e.username,
      password: e.password
    }, 'POST').then(res => {
      if (res.code === '100') {
        // 存储用户信息
        let jsonshopInfo = '';
        wx.setStorageSync('login_type', 1); // 缓存登录的方式1为正式登录，2为演示登录
        if (JSON.parse(res.data[0].floor) === 0) {
          if (res.data[0].jsonshop === '') {
            jsonshopInfo = 0;
          } else {
            jsonshopInfo = res.data[0].jsonshop
          }
        } else {
          jsonshopInfo = res.data[0].departmentid
        }
        let userInfo = {
          user_id: res.data[0].id,
          user_name: res.data[0].name,
          sex: res.data[0].sex,
          orcode: res.data[0].orcode,
          ismanage: res.data[0].ismanage,
          authority: JSON.parse(res.data[0].modelarr) || '',
          floor: JSON.parse(res.data[0].floor),
          departmentid: res.data[0].departmentid,
          departmentname: res.data[0].departmentname,
          jsonshop: jsonshopInfo
        }
        res.data[1] = userInfo
        wx.setStorageSync('userInfo', userInfo)
        // wx.setStorageSync('token', res.token)
        resolve(res)
      } else {
        reject(res)
      }
    }).catch((err) => {
      reject(err)
    })
    // }).catch((err) => {
    //   reject(err)
    // })
  })
}
/**
 * 调用微信手机号登录
 */
function loginByphoneWeixin (e) {
  // let code = null
  return new Promise(function (resolve, reject) {
    // 把用户信息传给后台，存入数据库，并计算一个token给前台存起来
    return util.request(api.AuthgetphoneNumber, {
      encryptedData: e.encryptedData,
      iv: e.iv,
      token: e.token,
      orcode: e.orcode
    }, 'POST').then(res => {
      // console.log(res)
      if (res.code === '200') {
        // 存储用户信息
        let jsonshopInfo = '';
        wx.setStorageSync('login_type', 1); // 缓存登录的方式1为正式登录，2为演示登录
        if (JSON.parse(res.data[0].floor) === 0) {
          if (res.data[0].jsonshop === '') {
            jsonshopInfo = 0;
          } else {
            jsonshopInfo = res.data[0].jsonshop
          }
        } else {
          jsonshopInfo = res.data[0].departmentid
        }
        let userInfo = {
          user_id: res.data[0].id,
          user_name: res.data[0].name,
          sex: res.data[0].sex,
          orcode: res.data[0].orcode,
          ismanage: res.data[0].ismanage,
          authority: JSON.parse(res.data[0].modelarr) || '',
          floor: JSON.parse(res.data[0].floor),
          departmentid: res.data[0].departmentid,
          departmentname: res.data[0].departmentname,
          jsonshop: jsonshopInfo
        }
        res.data[1] = userInfo
        wx.setStorageSync('userInfo', userInfo)
        // wx.setStorageSync('token', res.token)
        resolve(res)
      } else {
        reject(res)
      }
    })
  })
}
/**
 * 用户更改密码
 */
function changePass (obj) {
  return new Promise(function (resolve, reject) {
    return util.request(api.ChangePass, obj, 'POST').then((res) => {
      if (res.code === '200') {
        resolve(res)
      } else {
        reject(res)
      }
    }).catch((err) => {
      reject(err)
    })
  })
}
/**
 * 判断用户是否登录
 */
function checkLogin () {
  return new Promise(function (resolve, reject) {
    if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) {
      util.checkSession().then(() => {
        resolve(true)
      }).catch(() => {
        reject(false)
      })
    } else {
      reject(false)
    }
  })
}

const user = {
  loginBypassword,
  loginByphoneWeixin,
  checkLogin,
  changePass
}

export default user
